package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.User;
import com.qfedu.entity.Vip;
import com.qfedu.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/vip")
public class VipController {

    @Autowired
    private VipService vipService;

    /**
     * 查询所有vip信息
     * @return
     */
    @RequestMapping("/queryVip.do")
    public ResponseData queryVip(int page, int limit) {
        ResponseData<Object> responseData = new ResponseData<>();

        PageHelper.startPage(page,limit);

        List<Vip> vips = vipService.queryVip();

        PageInfo<Vip> pageInfo = new PageInfo<>(vips);
        int total = (int) pageInfo.getTotal();

        if (vips != null && vips.size() > 0) {
            responseData.setCode(0);
            responseData.setMsg("vip数据");
            responseData.setCount(total);
            responseData.setData(vips);
        }

        return responseData;
    }

    /**
     * 添加vip
     * @return
     */
    @RequestMapping("/add.do")
    public ResponseData addVip(String content, HttpServletRequest request) {
        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");
        String no = user.getNo();

        Vip vip = new Vip();
        vip.setContent(content);
        vip.setU_no(no);

        int i = vipService.addVip(vip);

        ResponseData<Object> responseData = new ResponseData<>();
        if (i > 0) {
            responseData.setCode(0);
            responseData.setMsg("申请成功");
        } else {
            responseData.setCode(1);
            responseData.setMsg("申请失败");
        }

        return responseData;
    }

}
